package com.wu.controller;

import com.wu.pojo.Classify;
import com.wu.pojo.view.ClassifyView;
import com.wu.response.CommonReturnType;
import com.wu.service.ArticleClassifyService;
import com.wu.service.ClassifyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 吴磊
 * @program: blog
 * @create: 2021-04-22 17:53
 */
@RestController
@RequestMapping("/classify")
@Api(tags = "文章专栏")
public class ClassifyController {

    @Autowired
    private ArticleClassifyService articleClassifyService;

    @Autowired
    private ClassifyService classifyService;

    /**
     * 获取用户自己全部的文章分类
     *
     * @return
     */
    @ApiOperation(value = "获取用户自己的文章分类")
    @ApiImplicitParams({@ApiImplicitParam(name = "userId", value = "用户ID，如果为空就是获取自己的", dataTypeClass = String.class)})
    @RequestMapping(value = "/getAllUserClassify", method = RequestMethod.POST)
    public CommonReturnType getAllUserClassify(@RequestParam(name = "userId", required = false) String userId) {
        List<Classify> classifyByUserId = classifyService.getClassifyByUserId(userId);
        return CommonReturnType.create(classifyByUserId);
    }


    /**
     * 获取文章分类及其文章数
     *
     * @param classifyName
     * @return
     */
    @ApiOperation(value = "获取用户文章分类及其文章数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "classifyName", value = "文章分类名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "userId", value = "用户ID ", required = true, dataTypeClass = String.class)
    })
    @RequestMapping(value = "/getClassify", method = RequestMethod.POST)
    public CommonReturnType getClassify(@RequestParam(name = "classifyName", required = false) String classifyName,
                                        @RequestParam(name = "userId") String userId) {
        if (StringUtils.isEmpty(classifyName)) {
            classifyName = null;
        }
        List<ClassifyView> viewList = articleClassifyService.getClassifyWithArticleNumber(classifyName, userId);
        return CommonReturnType.create(viewList);
    }


    /**
     * 获取所有的文章分类
     *
     * @return
     */
    @ApiOperation(value = "获取所有的文章分类")
    @RequestMapping(value = "/getAllClassify", method = RequestMethod.POST)
    public CommonReturnType getAllClassify() {
        List<Classify> allClassify = classifyService.getAllClassify();
        return CommonReturnType.create(allClassify);
    }


    /**
     * 获取所有文章数不为空的文章分类
     *
     * @return
     */
    @ApiOperation(value = "获取所有文章数不为空的文章分类")
    @RequestMapping(value = "/getAllClassifyNotNull", method = RequestMethod.POST)
    public CommonReturnType getAllClassifyNotNull() {
        List<Classify> allClassify = classifyService.getAllArticleNotNullClassify();
        return CommonReturnType.create(allClassify);
    }

}

